//
//  SettingsViewController.m
//  厚积薄发
//
//  Created by 侯振兴 on 14/12/31.
//  Copyright (c) 2014年 侯振兴. All rights reserved.
//

#import "SettingsViewController.h"
#import "WenZiViewController.h"
#import "OpinionViewController.h"
#import "OurViewController.h"
#import "OffLineViewController.h"
#import "DengLuViewController.h"
#import <ShareSDK/ShareSDK.h>
#import "SDImageCache.h"

#import "DetailBace.h"
#import "DetailModel.h"
@interface SettingsViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) SDImageCache *SDimage;

@end

@implementation SettingsViewController


- (void)dealloc
{
    [super dealloc];
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.tableArray = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 45, 60)];
    label.font = [UIFont fontWithName:@"TimesNewRomanPS-BoldMT" size:20];
    label.text = @"个人设置";
    label.textColor = [UIColor whiteColor];
    self.navigationItem.titleView = label;
    
    [self drawTableView];
}
-(void)drawTableView{
    UITableView *tview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 120) style:UITableViewStyleGrouped];
    [tview setDelegate:self];
    [tview setDataSource:self];
    [self.view addSubview:tview];
    [tview release];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if ( section == 1) {
        return 2;
    } else if (section == 2) {
        return 3;
    } else {
        return 1;
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    cell.textLabel.textAlignment = NSTextAlignmentLeft;
    switch (indexPath.section) {
        case 0:
            cell.textLabel.text = @"点击登陆";
            cell.imageView.image = [UIImage imageNamed:@"geren.png"];
            break;
        case 1:
            if (indexPath.row == 0) {
                cell.textLabel.text =  @"清空缓存";
                cell.imageView.image = [UIImage imageNamed:@"qingkong.png"];
            } else {
                cell.textLabel.text = @"清空'我的馆藏'";
                cell.imageView.image = [UIImage imageNamed:@"qingkong2.png"];
            }
            break;
        case 2:
            if(indexPath.row == 0)
            {
                cell.textLabel.text =  @"检查更新";
                cell.imageView.image = [UIImage imageNamed:@"gengxin.png"];
            } else if (indexPath.row == 1) {
                cell.textLabel.text = @"意见反馈";
                cell.imageView.image = [UIImage imageNamed:@"fankui.png"];
            } else {
                cell.textLabel.text = @"关于我们";
                cell.imageView.image = [UIImage imageNamed:@"guanyuwomen.png"];
            }
            break;
        case 3:
            cell.textLabel.text = @"退出登陆";
            cell.textLabel.textAlignment = NSTextAlignmentCenter;
            break;
        default:
            break;
    }
#pragma mark -
#pragma mark TableViewGroup下面的Button创建
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        NSUserDefaults *user1 = [NSUserDefaults standardUserDefaults];
        NSString *judge = [user1 objectForKey:@"login"];
        if ([judge isEqualToString:@"yes"]) {
            self.alert3 = [[UIAlertView alloc] initWithTitle:@"已登录" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
            [_alert3 show];
            [NSTimer scheduledTimerWithTimeInterval:1.5f target:self selector:@selector(timerAction:) userInfo:nil repeats:NO];
            [_alert3 release];
            
        } else {
        DengLuViewController *dengController = [[DengLuViewController alloc] init];
        [self.navigationController pushViewController:dengController animated:YES];
        [dengController release];
        }
    }  else if (indexPath.section == 1 && indexPath.row == 0) {
        self.SDimage = [[[SDImageCache alloc] init] autorelease];
        float clean = [self.SDimage checkTmpSize];
        NSString *sumMemory = [NSString stringWithFormat:@"已经缓存%.2fM了,是否清除", clean];
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"清除缓存" message:sumMemory delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"返回", nil];
        [alertView show];
        [alertView release];
    } else if (indexPath.section == 1 && indexPath.row == 1) {
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        [DetailBace updateDetail_tit:@"" detail_id:[[user objectForKey:@"nid"] integerValue] detail_http:@""];
        
        self.alert2 = [[UIAlertView alloc] initWithTitle:@"清理成功" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
        [_alert2 show];
        
        [NSTimer scheduledTimerWithTimeInterval:1.5f target:self selector:@selector(timerAction:) userInfo:nil repeats:NO];
        [_alert2 release];
    }
    else if (indexPath.section == 2 && indexPath.row == 0) {
        self.alert = [[UIAlertView alloc] initWithTitle:@"已经是最新版本了哦!" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
        [_alert show];
        
        [NSTimer scheduledTimerWithTimeInterval:1.5f target:self selector:@selector(timerAction:) userInfo:nil repeats:NO];
        
        [_alert release];
        
    } else if (indexPath.section == 2 && indexPath.row == 1) {
        OpinionViewController *opinionController = [[OpinionViewController alloc] init];
        [self.navigationController pushViewController:opinionController animated:YES];
        [opinionController release];
    } else if (indexPath.section == 2 && indexPath.row == 2) {
        OurViewController *ourController = [[OurViewController alloc] init];
        [self.navigationController pushViewController:ourController animated:YES];
        [ourController release];
    } else if (indexPath.section == 3) {
        [ShareSDK cancelAuthWithType:ShareTypeSinaWeibo];
        [self.navigationController popViewControllerAnimated:NO];
        
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        NSString *nsss = [user objectForKey:@"login"];
        if ([nsss isEqualToString:@"yes"]) {
        [user setObject:@"no" forKey:@"login"];
            [user removeObjectForKey:@"name"];
            [user removeObjectForKey:@"image"];
            [user setObject:nil forKey:@"nid"];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"denglu" object:self userInfo:nil];
            self.alert4 = [[UIAlertView alloc] initWithTitle:@"退出成功" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
            [NSTimer scheduledTimerWithTimeInterval:1.5f target:self selector:@selector(timerAction:) userInfo:nil repeats:NO];
            [_alert4 show];
            [_alert4 release];
        } else {
            self.alert5 = [[UIAlertView alloc] initWithTitle:@"您还没有登录" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
            [NSTimer scheduledTimerWithTimeInterval:1.5f target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
            [_alert5 show];
            [_alert5 release];
        }
    }
    
}

- (void)timerAction:(NSTimer *)timer
{
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"Rinse" object:self userInfo:nil];
    [_alert dismissWithClickedButtonIndex:0 animated:YES];
    [_alert2 dismissWithClickedButtonIndex:0 animated:YES];
    [_alertView dismissWithClickedButtonIndex:0 animated:YES];
    [_alert3 dismissWithClickedButtonIndex:0 animated:YES];
    [_alert4 dismissWithClickedButtonIndex:0 animated:YES];
    [_alert5 dismissWithClickedButtonIndex:0 animated:YES];
}

- (void)btLogOutTouchUpInside:(id)sender
{
    
}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        [_SDimage clearMemory];
        [_SDimage clearDisk];
        self.alertView = [[UIAlertView alloc] initWithTitle:@"清理成功" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
        [_alertView show];
        [NSTimer scheduledTimerWithTimeInterval:1.5f target:self selector:@selector(timerAction:) userInfo:nil repeats:NO];
        [_alertView release];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
